// 引入express，并获得express的实例
const express = require('express')
const path = require("path")
const app = express()
// const axios = require("axios");
const http = require('http');
// const userList = require("./store/userList.json")
// const robotReply = require("./store/robotReply.json")
const cors = require('cors')

// 用http模块创建一个服务并把express的实例挂载上去
const server = http.Server(app);
// 引入socket.io并立即实例化，把server挂载上去
const io = require('socket.io')(server, {
    // 配置跨域
	cors: {
		origin: "http://localhost:5173",
		methods: ["GET", "POST"]
	}
});

app.use(cors());

let activeUserList = []
const userList = [{
	userId: '1',
	userName: '张三',
}, {
	userId: '2',
	userName: '李四',
}, {
	userId: '3',
	userName: '王五',
}, {
    userId: '4',
	userName: '赵六',
}, {
    userId: '5',
	userName: '钱七',
}]
const friendList = [{
	id: '1',
    userId: '1',
	friendId: '2',
	roomId: '1',
}, {
	id: '2',
    userId: '1',
	friendId: '3',
	roomId: '2',
}, {
	id: '3',
    userId: '2',
	friendId: '4',
	roomId: '3',
}, {
	id: '4',
    userId: '3',
	friendId: '5',
	roomId: '4',
}, {
	id: '5',
    userId: '4',
	friendId: '1',
	roomId: '5',
}, {
	id: '6',
    userId: '5',
	friendId: '2',
	roomId: '6',
}]
const roomList = []
const msgList = []
const user_room = []
const room_msg = []

const initChat = (socket) => {
	const { userId } = socket.handshake.query
	const _friendList = friendList.filter(item => item.userId === userId)
	const userFriendList = _friendList.reduce((acc, item) => {
		const { friendId } = item
		const user = userList.find(item => item.userId === friendId)
		acc.push(user)
		return acc
	}, [])
}
io.on('connection', socket => {
	initChat(socket)
	socket.on("sendMsg", data => {
		console.log('sendMsg')
	})
	socket.on("disconnect", (reason) => {
		console.log('disconnect')
	});
});

server.listen(3000, function () {
	console.log('api server running at http://localhost:3000');
});